package org.yh.leetcode;

/**
 * @Author: yh
 * @Description:
 * @Date: Created in 15:07 2019/5/13
 * @Modified By:
 */
public class Test5 {

    //给出一个 32 位的有符号整数，你需要将这个整数中每位上的数字进行反转。
    //思路：转成字符（感觉这个办法很傻屌）
    public int reverse(int x) {
        String temp = String.valueOf(x);
        String temp2 = "";
        if (temp.charAt(0) == '-') {
            temp = temp.substring(1, temp.length());
            temp2 = "-";
        }
        for (int i = temp.length(); i > 0; i--) {
            temp2 += temp.charAt(i - 1);
        }
        Long aLong = Long.valueOf(temp2);
        if (aLong < -(2 << 30) || aLong > (2 << 30) - 1) {
            return 0;
        }
        return Integer.valueOf(temp2);
    }

    //思路2：取模算法
    //public int reverse2(int x) {
    //
    //}
}
